/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action.staff;

import static com.opensymphony.xwork2.Action.INPUT;
import static com.opensymphony.xwork2.Action.SUCCESS;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator;
import com.opensymphony.xwork2.validator.annotations.StringLengthFieldValidator;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;
import database.StaffDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import model.Customer;
import model.Staff;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;

/**
 *
 * @author Trang
 */
@Namespace("/Administration/Staff")
public class StaffAction extends ActionSupport implements ModelDriven<Staff>{
    private Integer sid;
    private List<Staff> staffs = new ArrayList<Staff>();
    private Staff staff = new Staff();
    private StaffDAO staffDAO = new StaffDAO();
    private Integer hiddenStaffID;
    private String hiddentRole;
    
    
    public Staff getStaff()
    {
        return staff;
    }
    public void setStaff(Staff staff)
    {
        this.staff = staff;
    }
    
    public List<Staff> getStaffs()
    {
        return staffs;
    }
    
    public void setStaffs(List<Staff> staffs)
    {
        this.staffs = staffs;
    }
    
    public Integer getSid()
    {
        return sid;
    }
    
    public void setSid(Integer sid)
    {
        this.sid = sid;
    }
    
   
    @Override
     @Action(
            value = "edit-staff/*",
            results = {
        @Result(name = "success", location = "/manageStaff/w_editstaff.jsp")},
            params = {"sid", "{1}"})
    public String execute() {
        
        this.staff =  staffDAO.getStaffByStaffId(sid);  
        setHiddenStaffID(staff.getIdStaff());
        //setHiddentRole(staff.getRole());
        return SUCCESS;
    }
     
     @Action(value = "editInfor", 
             results = {
        @Result(name = "success", location = "/manageStaff/w_administrator.jsp"),   
        @Result(name = "input", location = "/manageStaff/w_editstaff.jsp")}
             )
     public String editInfor()
     {      
         System.out.println(hiddenStaffID);
         staff.setIdStaff(hiddenStaffID);
         System.out.println(staff.getCloseTime());
             if(staffDAO.updateStaff(staff))
             {
                 staffs = staffDAO.getAllStaff();
             addActionMessage("Your Profile has been changed successfully.");
             return SUCCESS;
             }
             else
             {
             addActionError("Cannot Update Profile. Please try again later.");
             return INPUT; 
             }
//         }
     } 
    
    
    @Override
    @VisitorFieldValidator(message = "", appendPrefix = false)
    public Staff getModel() {
       return staff;
    }

    /**
     * @return the hiddenStaffID
     */
    public Integer getHiddenStaffID() {
        return hiddenStaffID;
    }

    /**
     * @param hiddenStaffID the hiddenStaffID to set
     */
    public void setHiddenStaffID(Integer hiddenStaffID) {
        this.hiddenStaffID = hiddenStaffID;
    }

    /**
     * @return the hiddentRole
     */
    public String getHiddentRole() {
        return hiddentRole;
    }

    /**
     * @param hiddentRole the hiddentRole to set
     */
    public void setHiddentRole(String hiddentRole) {
        this.hiddentRole = hiddentRole;
    }
}
